package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.NormalOrder;
import com.woniuxy.entity.ReturnOrder;
import com.woniuxy.entity.SignOrder;
import com.woniuxy.entity.Waybill;
import com.woniuxy.entity.dto.SignOrderDTO;
import com.woniuxy.entity.dto.WaybillDTO;
import com.woniuxy.mapper.SignOrderMapper;
import com.woniuxy.service.SignOrderService;
import com.woniuxy.util.PageData;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import com.woniuxy.util.UserContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SignOrderServiceImpl implements SignOrderService {
    @Resource
    private SignOrderMapper signOrderMapper;
    @Override
    public ResponseData<?> querySignOrder(PageData<SignOrderDTO> pageData) {
        PageHelper.startPage(pageData.getPageNum(),pageData.getPageSize());
        List<SignOrderDTO> signOrderDTOS = signOrderMapper.querySignOrder(pageData.getParam());
        PageInfo pageInfo = new PageInfo(signOrderDTOS);
        return new ResponseData<>().ok(pageInfo);
    }

    @Override
    public ResponseData<?> handleSign(SignOrderDTO signOrderDTO) { //签收订单

//        if("3".equals(signOrderDTO.getStatus())||"6".equals(signOrderDTO.getStatus())){
//            return new ResponseData<>().fail(ResponseEnum.ORDER_ERROR);
//        }

        //dto中传回 orderId , status ,remarks,signCount,imgSrc
        SignOrder signOrder = new SignOrder();
        signOrder.setSignCount(signOrderDTO.getSignCount());
        signOrder.setOrderId(signOrderDTO.getOrderId());
        signOrder.setRemarks(signOrderDTO.getRemarks());
        signOrder.setImgSrc(signOrderDTO.getImgSrc());
        String status = signOrderDTO.getStatus();
        signOrder.setStatus(status);
        if("2".equals(status)){ //签收
            signOrder.setOrderStatus("3");
        }
        if("3".equals(status)){//拒绝
            signOrder.setOrderStatus("4");
        }
        signOrder.setPoweruserId(String.valueOf(UserContext.getUserId()));
        signOrderMapper.updateSignOrder(signOrder);

        //修改运单表状态
        NormalOrder normalOrder = signOrderMapper.queryByNorId(signOrderDTO.getOrderId());
        String waybillId = normalOrder.getWaybillId();
        Waybill waybill = new Waybill();
        waybill.setId(waybillId);
        if("2".equals(status)){ //签收
            waybill.setStatus("6");//已完成
        }
        if("3".equals(status)){//拒绝
            waybill.setStatus("7");//未完成
        }
        signOrderMapper.updateWaybillStatus(waybill);

        //修改订单表状态
        NormalOrder normalOrder1 = new NormalOrder();
        normalOrder1.setId(signOrderDTO.getOrderId());

        if("2".equals(status)){ //签收
            normalOrder1.setOrderStatus("3");//已签收
        }
        if("3".equals(status)){//拒绝
            normalOrder1.setOrderStatus("4");//已拒绝
        }
        signOrderMapper.updateNorStatus(normalOrder1);


        //同时生成回单表数据；
        ReturnOrder returnOrder = new ReturnOrder();
        returnOrder.setOrderId(signOrderDTO.getOrderId());
        returnOrder.setCustomerId(normalOrder.getCustomerId());
        returnOrder.setUserId(null);
        SignOrder signOrder1 = signOrderMapper.queryByOrderId(signOrderDTO.getOrderId());
        returnOrder.setSignOrderId(signOrder1.getId());
        returnOrder.setPickupAddressId(normalOrder.getPickupAddressId());
        returnOrder.setDeliveryAddressId(normalOrder.getDeliveryAddressId());
        signOrderMapper.addReturnOrder(returnOrder);

        //返回code码值
        if("2".equals(status)){
            return new ResponseData<>().ok();
        }
        if("3".equals(status)){
            return new ResponseData<>().fail(ResponseEnum.COMBINED_ORDER_ERROR2);
        }
        return null;
    }

    @Override
    public ResponseData<?> querySignOrderDetails(String orderId) {
        //查询详细信息
        SignOrderDTO signOrderDTO = signOrderMapper.querySignDetails(orderId);
        return new ResponseData<>().ok(signOrderDTO);
    }


    @Override
    public ResponseData<?> updateImg(SignOrderDTO signOrderDTO) {
        signOrderMapper.updateImg(signOrderDTO);
        return  new ResponseData<>().ok();
    }
}
